package com.wangsh.userscenter.head.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.common.controller.BaseController;
import com.wangsh.userscenter.common.util.ConstatFinalUtil;
import com.wangsh.userscenter.users.pojo.AUsers;
import com.wangsh.userscenter.users.pojo.AUsersEnum;
import com.wangsh.userscenter.users.pojo.AUsersExtend;
import com.wangsh.userscenter.users.service.IUsersOperService;
import com.wangsh.userscenter.users.service.IUsersService;
import com.wangsh.userscenter.util.ServiceCallableUtil;

/**
 * 未登陆页面
 * @author wangsh_sxt
 *
 */
@Controller
public class NoLoginController extends BaseController
{
	@Resource
	private IUsersService usersService ;
	@Resource
	private IUsersOperService usersOperService;
	
	/**
	 * 登陆
	 * @return
	 */
	/**
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/login.html")
	public String login(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		this.clearInfo(request);
		/* 接收页面参数 */
		String returnUrl = request.getParameter("returnUrl");
		if(returnUrl == null)
		{
			returnUrl = ""; 
		}
		AUsers users = (AUsers) this.findObjfromSession(request, "1");
		if(users != null)
		{
			/* 登陆成功处理页面跳转 */
			if(logSuccedReturnUrl(response, returnUrl, users))
			{
				return null ; 
			}
			/* 用户已经登陆 */
			return "redirect:/head/users/main.html" ; 
		}
		return "/login"; 
	}
	
	/**
	 * 登陆提交
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/loginSubmit.html")
	public String loginSubmit(HttpServletRequest request,HttpServletResponse response,Model model) throws IOException
	{
		HttpSession session = request.getSession();
		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		/* 登陆成功跳转的URL */
		String returnUrl = request.getParameter("returnUrl");
		
		/*图片验证码验证*/
		String randSess = session.getAttribute("rand") + "";
		
		/* 验证码输错次数 */
		int randFiledCount = 0 ;
		if(session.getAttribute("randFiledCount") != null )
		{
			randFiledCount = Integer.valueOf(session.getAttribute("randFiledCount") + "");
		}
		
		/* 移除验证码,防止重复使用 */
		session.removeAttribute("rand");
		
		/* 验证码相同 */
		if(randSess.equalsIgnoreCase(code) || 
				randFiledCount < Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.getString("session.randFailedCount")))
		{
			randFiledCount ++ ;  
			session.setAttribute("randFiledCount", randFiledCount);
			
			/* 查询db,处理业务逻辑 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			AUsers users = this.usersService.findOneUsersService(condMap);
			/* 检验邮箱 */
			if(users != null)
			{
				/* 将邮箱放到request中 */
				model.addAttribute("users",users);
				
				/* 取得当前的时间-数据库的时间,如果大于10分钟,将faildCount值改为0
				 * 超过时分钟,自动解锁
				 *  */
				if(users.getFailedTime() != null && 
						new Date().getTime() - users.getFailedTime().getTime() > 
						Long.valueOf(ConstatFinalUtil.CONFIG_JSON.get("password.error.lockTime") + ""))
				{
					users.setFailedCount(0);
				}
				
				/* 
				 * 密码不验证:
				 * ~次数大于最大次数,&& 时间在一个小时以内
				 *  */
				if(!(users.getFailedCount() < Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.get("password.error.maxCount") + ""))
						&& 
						new Date().getTime() - users.getFailedTime().getTime() < 
						Long.valueOf(ConstatFinalUtil.CONFIG_JSON.get("password.error.lockTime") + ""))
				{
					Map<String, String> infoMap = new HashMap<String, String>() ;
					infoMap.put("faildCount", users.getFailedCount() + "");
					/* 锁定一段时间的提示 */
					info = this.fileUtil.replaceOperator(ConstatFinalUtil.INFO_JSON.getString("8"), infoMap);
					
					model.addAttribute("info", info);
					return "login" ; 
				}
				
				/* 检验密码 */
				if(this.encryptUtil.checkStr(password, users.getPassword()))
				{
					/* 校验状态 */
					if(users.getStatus() == 1)
					{
						/* 登陆成功 */
						session.setAttribute("users", users);
						session.setAttribute("lastLoginTime", users.getLastLoginTime());
						
						/* 修改管理员操作 */
						users.setLoginCount(users.getLoginCount() + 1);
						users.setLastLoginTime(new Date());
						
						/* 输入正确的时候,要重置 */
						users.setFailedCount(0);
						
						/* 将验证码的输错次数从session中移除 */
						session.removeAttribute("randFiledCount");
						
						/* 不更新扩展信息 */
						users.setUsersExtend(null);
						this.usersService.updateOneUsersService(users);
						
						/* 登陆成功处理页面跳转 */
						if(logSuccedReturnUrl(response, returnUrl, users))
						{
							return null ; 
						}
						return "redirect:/head/users/main.html" ; 
					}else
					{
						/* "15:账户状态为:" + users.getStatusStr() */
						Map<String, String> infoMap = new HashMap<String,String>() ; 
						infoMap.put("status", users.getStatusStr());
						this.info = this.fileUtil.replaceOperator(ConstatFinalUtil.INFO_JSON.getString("15"), infoMap);
					}
				}else
				{
					/* 4:密码不正确 */
					this.info = ConstatFinalUtil.INFO_JSON.getString("4") ; 
					
					/* 
					 * users.getFaildTime():赋值的问题
					 * 只有第一次输错的时间才赋值
					 *  */
					if(users.getFailedCount() == 0 )
					{
						/* 第二次输错的时候,不执行 */
						users.setFailedTime(new Date());
					}
					if(users.getFailedCount() < Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.get("password.error.maxCount") + "")
						&& 
						new Date().getTime() - users.getFailedTime().getTime() < 
						Long.valueOf(ConstatFinalUtil.CONFIG_JSON.get("password.error.lockTime") + ""))
					{
						/* 
						 * 密码的失败次数小于最大的次数
						 * 密码的时间必须在一个小时以内
						 * 
						 * 每登陆失败一次,faildCount+1, */
						users.setFailedCount(users.getFailedCount() + 1);
						/* 更新失败次数和时间 */
						this.usersService.updateOneUsersService(users);
						
						Map<String, String> infoMap = new HashMap<String, String>() ;
						infoMap.put("faildCount", users.getFailedCount() + "");
						/* 锁定一段时间的提示 */
						info += this.fileUtil.replaceOperator(ConstatFinalUtil.INFO_JSON.getString("22"), infoMap);
					}else
					{
						Map<String, String> infoMap = new HashMap<String, String>() ;
						infoMap.put("faildCount", users.getFailedCount() + "");
						/* 锁定一段时间的提示 */
						info = this.fileUtil.replaceOperator(ConstatFinalUtil.INFO_JSON.getString("8"), infoMap);
					}
				}
			}else
			{
				/* 5:邮箱不存在 */
				this.info = ConstatFinalUtil.INFO_JSON.getString("5"); 
			}
		}else
		{
			/* 6:验证码不正确 */
			this.info = ConstatFinalUtil.INFO_JSON.getString("6"); 
		}
		request.setAttribute("returnUrl", returnUrl);
		request.setAttribute("info", info);
		return "login";
	}
	
	/**
	 * 单点登陆,returnUrl处理
	 * @param response
	 * @param returnUrl
	 * @param users
	 * @return
	 * @throws IOException
	 */
	private boolean logSuccedReturnUrl(HttpServletResponse response, String returnUrl, AUsers users) throws IOException
	{
		if(returnUrl == null)
		{
			returnUrl = ""; 
		}
		
		if(!"".equalsIgnoreCase(returnUrl))
		{
			/* 为ReturnUrl带上一个Token:
			 * 令牌:唯一标识 
			 * 原来的returnUrl:http://www.baidu.com
			 * http://www.baidu.com?token=uuid
			 * 
			 * 原来的returnUrl:http://www.baidu.com?id=10
			 * http://www.baidu.com?id=10&a=20&token=uuid
			 * */
			this.usersOperService.addUsersTokenService(users);
			String token = users.getTokenStr() ; 
			
			/* token的验证字符串 */
			String tokenStr = "token=" + token ; 
			if(returnUrl.indexOf("&") == -1 && returnUrl.indexOf("?") == -1)
			{
				/* 找到?,并且找到ticket */
				returnUrl += "?" + tokenStr ;
			}else
			{
				returnUrl += "&" + tokenStr ;
			}
		}
		
		/* 如果带了returnUrl,直接跳转returnUrl */
		if(!"".equalsIgnoreCase(returnUrl))
		{
			response.sendRedirect(returnUrl);
			return true ; 
		}
		return false ; 
	}
	
	/**
	 * 注册
	 * @return
	 */
	@RequestMapping("/regsiter.html")
	public String regsiter()
	{
		return "/regsiter"; 
	}
	
	/**
	 * 注册提交
	 * @return
	 */
	@RequestMapping("/regsiterSubmit.html")
	public String regsiterSubmit(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		/* 接收参数 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		
		/*图片验证码验证*/
		String randSess = session.getAttribute("rand") + "";
		
		/* 移除验证码,防止重复使用 */
		session.removeAttribute("rand");
		
		/* 验证码相同 */
		if(randSess.equalsIgnoreCase(code))
		{
			AUsers users = new AUsers();
			users.setEmail(email);
			users.setPassword(this.encryptUtil.encodeStr(password));
			users.setStatus(AUsersEnum.STATUS_ENABLE.getStatus());
			users.setCreateTime(new Date());
			users.setUpdateTime(new Date());
			users.setLastLoginTime(new Date());
			JSONObject dbJSON = this.usersService.saveOneUsersService(users);
			if("0".equalsIgnoreCase(dbJSON.getString("code")))
			{
				/* 0:成功 */
				info = dbJSON.getString("info") ; 
				/* 发送注册邮件 */
				return this.emailVerfySend(request);
			}else 
			{
				info = dbJSON.getString("info") ; 
			}
		}else
		{
			/* 6:验证码不正确 */
			this.info = ConstatFinalUtil.INFO_JSON.getString("6"); 
		}
		request.setAttribute("info", info);
		return this.regsiter() ; 
	}
	
	/**
	 * 邮箱验证重新发送邮件
	 * @return
	 */
	@RequestMapping("/emailVerfySend.html")
	public String emailVerfySend(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		/* 根据邮箱来操作 */
		String email = request.getParameter("email");
		if(email == null)
		{
			email = "" ; 
		}
		
		if(!"".equalsIgnoreCase(email))
		{
			Map<String, Object> condMap = new HashMap<String,Object>();
			condMap.put("email", email);
			AUsers users = this.usersService.findOneUsersService(condMap);
			if(users != null)
			{
				if(users.getEmailStatus() != AUsersEnum.EMAILSTATUS_PASSED.getStatus())
				{
					AUsersExtend usersExtend = users.getUsersExtend(); 
					usersExtend.setSendEmailTime(new Date());
					String uuid = UUID.randomUUID().toString() ;
					/* 加密字符串:  db存储为:sha256(邮箱+时间+私钥)*/
					String souStr = email + uuid; 
					ConstatFinalUtil.SYS_LOGGER.info("--sourceSode--:{}",souStr);
					souStr = DigestUtils.sha256Hex(souStr);
					usersExtend.setSendEmailCode(uuid);
					
					/* 发送邮件 */
					Map<String, String> emailMap = new HashMap<String, String>();
					emailMap.put("date", this.dateFormatUtil.dateTimeStr(new Date()));
					/* 邮箱验证的url */
					emailMap.put("verifyHref", ConstatFinalUtil.CONFIG_JSON.getString("website.url") + "/emailVerfy.html?code=" + 
							souStr + "&email=" + users.getEmail());
					emailMap.put("websiteurl", ConstatFinalUtil.CONFIG_JSON.getString("website.url"));
					
					String subject = "邮箱认证 - 用户中心 " ; 
					emailMap.put("email", users.getEmail());
					emailMap.put("subject", subject);
					
					InputStream is = ConstatFinalUtil.class.getResourceAsStream("/template/email/emailVerify.html");
					String content = this.fileUtil.readFile(is, ConstatFinalUtil.CHARSET, emailMap);
					
					List<Callable<Integer>> callList = new ArrayList<Callable<Integer>>();
					Map<String, Object> paramsMap = new HashMap<String, Object>();
					
					paramsMap.put("springEmailUtil", springEmailUtil);
					paramsMap.put("email", emailMap.get("email") + "");
					paramsMap.put("subject", subject);
					paramsMap.put("content", content);
					
					ServiceCallableUtil multiThreadUtil = new ServiceCallableUtil();
					multiThreadUtil.setParamsMap(paramsMap);
					multiThreadUtil.setOperType("sendEmail");
					callList.add(multiThreadUtil);
					
					//固定设置线程数为
					ExecutorService exeSvc = Executors.newFixedThreadPool(callList.size());
					try
					{
						exeSvc.submit(multiThreadUtil);
					} catch (Exception e)
					{
						ConstatFinalUtil.SYS_LOGGER.error("线程池报错了", e);
					}finally
					{
						exeSvc.shutdown();
					}
					
					JSONObject dbJSON = this.usersService.updateOneUsersExtendService(usersExtend);
					ConstatFinalUtil.SYS_LOGGER.info("邮箱验证:发送邮件,db执行结果:{}",dbJSON);
					info = "邮件已经发送,请查收;<a href='"+ request.getContextPath() +"/emailVerfySend.html?email="+ email +"'>重新发送</a>" ; 
				}else
				{
					/*info = "邮箱已经认证" ;*/
					/* 18:邮箱未注册*/
					info = ConstatFinalUtil.INFO_JSON.getString("18") ;
				}
			}else 
			{
				/* 17:邮箱未注册*/
				info = ConstatFinalUtil.INFO_JSON.getString("17") ;
			}
		}else
		{
			/* 16:邮箱不能为空*/
			info = ConstatFinalUtil.INFO_JSON.getString("16") ;
		}
		
		session.setAttribute("info", info);
		return "redirect:/commonInfo.html" ; 
	}
	
	/**
	 * 验证邮箱
	 * @param request
	 * @return
	 */
	@RequestMapping("/emailVerfy.html")
	public String emailVerfy(HttpServletRequest request)
	{
		HttpSession session = request.getSession() ;
		String email = request.getParameter("email");
		String code = request.getParameter("code");
		if(email == null)
		{
			email = "" ; 
		}
		
		if(!"".equalsIgnoreCase(email))
		{
			Map<String, Object> condMap = new HashMap<String,Object>();
			condMap.put("email", email);
			AUsers users = this.usersService.findOneUsersService(condMap);
			if(users != null)
			{
				boolean flag = true  ;
				AUsersExtend usersExtend = users.getUsersExtend();
				Calendar calendar = Calendar.getInstance();
				String uuid = usersExtend.getSendEmailCode() ;
				
				/* 加密字符串:  db存储为:sha256(邮箱+时间+私钥)*/
				String souStr = email + uuid; 
				ConstatFinalUtil.SYS_LOGGER.info("--sourceSode--:{}",souStr);
				souStr = DigestUtils.sha256Hex(souStr);
				
				if((calendar.getTimeInMillis() - usersExtend.getSendEmailTime().getTime() > ConstatFinalUtil.SECOND * 60 * 60 * 24))
				{
					/* 验证邮箱时间 */
					/*info = "邮箱链接已经超过一天" ;*/
					info = ConstatFinalUtil.INFO_JSON.getString("19");
					flag = false ; 
				}else if(!souStr.equalsIgnoreCase(code))
				{
					/* 验证邮件的合法性 */
					/*info = "邮箱链接已经被篡改" ;*/
					info = ConstatFinalUtil.INFO_JSON.getString("20");
					flag = false; 
				}else if(users.getEmailStatus() == AUsersEnum.EMAILSTATUS_PASSED.getStatus())
				{
					/* 验证邮箱是否被验证过 */
					flag = false; 
					/*info = "邮箱已经认证" ;*/
					info = ConstatFinalUtil.INFO_JSON.getString("18");
				}
				
				if(flag)
				{
					usersExtend.setSendEmailTime(new Date());
					/* 加密字符串:  db存储为:sha256(邮箱+时间+私钥)*/
					uuid = UUID.randomUUID().toString() ;
					usersExtend.setSendEmailCode(uuid);
					
					/* 修改邮箱状态 */
					users.setEmailStatus(AUsersEnum.EMAILSTATUS_PASSED.getStatus());
					usersExtend.setUpdateTime(new Date());
					JSONObject dbJSON = this.usersService.updateOneUsersService(users);
					ConstatFinalUtil.SYS_LOGGER.info("邮箱验证结果:邮箱:{},db执行结果:{}" , email , dbJSON);
					info = "邮箱验证成功" ;
				}				
			}else 
			{
				/*info = "邮箱未注册"; */
				/* 17:邮箱未注册*/
				info = ConstatFinalUtil.INFO_JSON.getString("17") ;
			}
		}else
		{
			/*info = "邮箱不能为空" ; */
			/* 16:邮箱不能为空*/
			info = ConstatFinalUtil.INFO_JSON.getString("16") ;
		}
		session.setAttribute("info", info);
		return "redirect:/commonInfo.html" ; 
	}
	
	/**
	 * 忘记密码
	 * @return
	 */
	@RequestMapping("/forgetPass.html")
	public String forgetPass(HttpServletRequest request)
	{
		return "/forgetPass" ; 
	}
	
	/**
	 * 忘记密码提交
	 * @return
	 */
	@RequestMapping("/forgetPassSubmit.html")
	public String forgetPassSubmit(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		/* 根据邮箱来操作 */
		String email = request.getParameter("email");
		if(email == null)
		{
			email = "" ; 
		}
		
		if(!"".equalsIgnoreCase(email))
		{
			String code = request.getParameter("code");
			
			/*图片验证码验证*/
			String randSess = session.getAttribute("rand") + "";
			
			/* 移除验证码,防止重复使用 */
			session.removeAttribute("rand");
			
			/* 验证码相同 */
			if(randSess.equalsIgnoreCase(code))
			{
				Map<String, Object> condMap = new HashMap<String,Object>();
				condMap.put("email", email);
				AUsers users = this.usersService.findOneUsersService(condMap);
				if(users != null)
				{
					AUsersExtend usersExtend = users.getUsersExtend(); 
					usersExtend.setSendEmailTime(new Date());
					
					String uuid = UUID.randomUUID().toString() ; 
					/* 加密字符串:  db存储为:sha256(邮箱+时间+私钥)*/
					String souStr = email + uuid; 
					souStr = DigestUtils.sha256Hex(souStr);
					usersExtend.setSendEmailCode(uuid);
					
					/* 发送邮件 */
					Map<String, String> emailMap = new HashMap<String, String>();
					emailMap.put("date", this.dateFormatUtil.dateTimeStr(new Date()));
					/* 邮箱验证的url */
					emailMap.put("verifyHref", ConstatFinalUtil.CONFIG_JSON.getString("website.url") + "/resetPass.html?code=" + 
							souStr + "&email=" + users.getEmail());
					emailMap.put("websiteurl", ConstatFinalUtil.CONFIG_JSON.getString("website.url"));
					
					String subject = "重置密码 - 用户中心 " ; 
					emailMap.put("email", users.getEmail());
					emailMap.put("subject", subject);
					
					InputStream is = ConstatFinalUtil.class.getResourceAsStream("/template/email/resetPass.html");
					String content = this.fileUtil.readFile(is, ConstatFinalUtil.CHARSET, emailMap);
					
					List<Callable<Integer>> callList = new ArrayList<Callable<Integer>>();
					Map<String, Object> paramsMap = new HashMap<String, Object>();
					
					paramsMap.put("springEmailUtil", springEmailUtil);
					paramsMap.put("email", emailMap.get("email") + "");
					paramsMap.put("subject", subject);
					paramsMap.put("content", content);
					
					ServiceCallableUtil multiThreadUtil = new ServiceCallableUtil();
					multiThreadUtil.setParamsMap(paramsMap);
					multiThreadUtil.setOperType("sendEmail");
					callList.add(multiThreadUtil);
					
					//固定设置线程数为
					ExecutorService exeSvc = Executors.newFixedThreadPool(callList.size());
					try
					{
						exeSvc.submit(multiThreadUtil);
					} catch (Exception e)
					{
						ConstatFinalUtil.SYS_LOGGER.error("线程池报错了", e);
					}finally
					{
						exeSvc.shutdown();
					}
					
					JSONObject dbJSON = this.usersService.updateOneUsersExtendService(usersExtend);
					ConstatFinalUtil.SYS_LOGGER.info("忘记密码:发送邮件,db执行结果:{}",dbJSON);
					info = "邮件已经发送,请查收;<a href='"+ request.getContextPath() +"/emailVerfySend.html?email="+ email +"'>重新发送</a>" ; 
				}else
				{
					info = "验证码不正确" ;
				}
			}else 
			{
				/*info = "邮箱未注册"; */
				/* 17:邮箱未注册*/
				info = ConstatFinalUtil.INFO_JSON.getString("17") ;
			}
		}else
		{
			/* 16:邮箱不能为空 */
			info = ConstatFinalUtil.INFO_JSON.getString("16") ;
		}
		
		session.setAttribute("info", info);
		return this.forgetPass(request) ; 
	}
	
	/**
	 * 重置密码
	 * @return
	 */
	@RequestMapping("/resetPass.html")
	public String resetPass(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		/* 根据邮箱来操作 */
		String email = request.getParameter("email");
		String code = request.getParameter("code");
		if(email == null)
		{
			email = "" ; 
		}
		
		if(!"".equalsIgnoreCase(email))
		{
			Map<String, Object> condMap = new HashMap<String,Object>();
			condMap.put("email", email);
			AUsers users = this.usersService.findOneUsersService(condMap);
			if(users != null)
			{
				boolean flag = true  ;
				AUsersExtend usersExtend = users.getUsersExtend();
				Calendar calendar = Calendar.getInstance();
				String uuid = usersExtend.getSendEmailCode() ; 
				/* 加密字符串:  db存储为:sha256(邮箱+时间+私钥)*/
				String souStr = email + uuid; 
				souStr = DigestUtils.sha256Hex(souStr);
				
				if((calendar.getTimeInMillis() - usersExtend.getSendEmailTime().getTime() > ConstatFinalUtil.SECOND * 60 * 60 * 24))
				{
					/* 验证邮箱时间 */
					info = ConstatFinalUtil.INFO_JSON.getString("19");
					flag = false ; 
				}else if(!souStr.equalsIgnoreCase(code))
				{
					/* 验证邮件的合法性 */
					/*info = "邮箱链接已经被篡改" ;*/
					info = ConstatFinalUtil.INFO_JSON.getString("20");
					flag = false; 
				}
				
				if(flag)
				{
					info = ""; 
					request.setAttribute("info", info);
					request.setAttribute("users", users);
					//符合要求,跳转页面
					return "/resetPass" ; 
				}
			}else
			{
				/*info = "邮箱不存在" ;*/
				/* 5:邮箱不存在 */
				info = ConstatFinalUtil.INFO_JSON.getString("5") ; 
			}
		}else
		{
			/* 16:邮箱不能为空 */
			info = ConstatFinalUtil.INFO_JSON.getString("16") ; 
		}
		
		session.setAttribute("info", info);
		return "redirect:/commonInfo.html" ; 
	}
	
	/**
	 * 重置密码提交
	 * @return
	 */
	@RequestMapping("/resetPassSubmit.html")
	public String resetPassSubmit(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		/* 根据邮箱来操作 */
		String email = request.getParameter("email");
		if(email == null)
		{
			email = "" ; 
		}
		
		if(!"".equalsIgnoreCase(email))
		{
			String code = request.getParameter("code");
			
			/*图片验证码验证*/
			String randSess = session.getAttribute("rand") + "";
			
			/* 移除验证码,防止重复使用 */
			session.removeAttribute("rand");
			
			/* 验证码相同 */
			if(randSess.equalsIgnoreCase(code))
			{
				Map<String, Object> condMap = new HashMap<String,Object>();
				condMap.put("email", email);
				AUsers users = this.usersService.findOneUsersService(condMap);
				if(users != null)
				{
					boolean flag = true  ;
					String password = request.getParameter("password");
					users.setPassword(this.encryptUtil.encodeStr(password));
					if(flag)
					{
						AUsersExtend usersExtend = users.getUsersExtend() ; 
						usersExtend.setSendEmailTime(new Date());
						String uuid = UUID.randomUUID().toString() ; 
						usersExtend.setSendEmailCode(uuid);
						usersExtend.setUpdateTime(new Date());
						this.usersService.updateOneUsersExtendService(usersExtend);
						
						users.setUpdateTime(new Date());
						JSONObject dbJSON = this.usersService.updateOneUsersService(users);
						ConstatFinalUtil.SYS_LOGGER.info("重置密码:邮箱:{},db结果:{}" , email , dbJSON);
						info = "密码重置成功" ;
					}
				}else 
				{
					/* 17: info = "邮箱未注册"; */
					info = ConstatFinalUtil.INFO_JSON.getString("17");
				}
			}else
			{
				/* 6:验证码不正确 */
				this.info = ConstatFinalUtil.INFO_JSON.getString("6"); 
			}
		}else
		{
			/* 16:邮箱不能为空 */
			info = ConstatFinalUtil.INFO_JSON.getString("16") ; 
		}
		
		session.setAttribute("info", info);
		return "redirect:/commonInfo.html" ; 
	}
}
